//
//  MyWalletViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/5/25.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "MyWalletViewController.h"
#import "WalletConsumeHistoryCell.h"
#import "WalletHeaderCell.h"
#import "WalletFooterCell.h"
#import "TopUpViewController.h"

@interface MyWalletViewController ()<UITableViewDataSource, UITableViewDelegate, WalletHeaderCellDelegate>

@end

@implementation MyWalletViewController{
    
    TopUpViewController *topUpViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.historyArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBActions
- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource && Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    if(section == 0){
        
        return 275;
    }else if(section == 1){
        
        return 50;
    }else{
        
        return 44;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section == 0 || section == 2){
        
        return 1;
    }else{
        
        return self.historyArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *headerCellIdentifier = @"WalletHeaderCell";
    NSString *historyCellIdentifier = @"WalletConsumeHistoryCell";
    NSString *footererCellIdentifier = @"WalletFooterCell";
    
    NSInteger section = indexPath.section;
    
    switch (section) {
        case 0:
        {
            WalletHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
            cell.delegate = self;
            [cell initDataWithRemainAmount:@"1000000"];
            return cell;
            break;
        }
        case 1:
        {
            WalletConsumeHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:historyCellIdentifier];
            return cell;
            break;
        }
        case 2:
        {
            WalletFooterCell *cell = [tableView dequeueReusableCellWithIdentifier:footererCellIdentifier];
            return cell;
            break;
        }
        default:
            break;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    
    if(section == 2){
        
        //加载更多
        [self.historyArray addObjectsFromArray:@[@"0",@"0",@"0",@"0",@"0"]];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - WalletHeaderCellDelegate
- (void)showTopUpView{
    
    topUpViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TopUpViewController"];
    [self.navigationController pushViewController:topUpViewController animated:YES];
}

@end
